//
//  BUViewController.m
//  Balance
//
//  Created by Calvin Kuo on 12-04-28.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//


/*
 Status: changed
 Last Merge Date: 05-30-2012
 Last Change Date: 
 Changes:
 1. check all and uncheck all
 
 */

#import "BUViewController.h"



@implementation BUViewController

@synthesize buTableView = _buTableView;
@synthesize buNames = _buNames;
@synthesize delegate;
@synthesize identifier;



#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.buNames = [NSMutableArray array];
    [_buNames addObject:@"Vision Operations"];
    [_buNames addObject:@"Vision Food"];
    [_buNames addObject:@"Vision Securities"];
    [_buNames addObject:@"Vision A"];
    [_buNames addObject:@"Vision B"];
    [_buNames addObject:@"Vision C"];
    [_buNames addObject:@"Vision D"];
    [_buNames addObject:@"Vision E"];
    [_buNames addObject:@"Vision F"];
    [_buNames addObject:@"Vision G"];
    [_buNames addObject:@"Vision H"];
    
    checkAll = NO;
    uncheckAll = NO;
    initial = YES;
 
}

- (void)viewDidUnload
{
   
    [self setBuTableView:nil];
    [super viewDidUnload];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [_buNames count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"BUCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSString *bu = [_buNames objectAtIndex:indexPath.row];
    cell.textLabel.text = bu;
    cell.textLabel.font = [UIFont systemFontOfSize:18.0];

    if ((!checkAll) && (!uncheckAll)){
        
        
        //default to select first 5 rows. only work when the app is first up.
        if ((initial) && (indexPath.row < 5)){
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            if (indexPath.row == 4) initial = NO;
        }
        else{
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        [self.delegate buViewController:self status:@"Multiple"];
        
        
    } else if ((checkAll) && (!uncheckAll)){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else if ((checkAll) && (!uncheckAll)){
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
   // NSLog(@"bu cell=%@", bu);

    
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
       
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (newCell.accessoryType == UITableViewCellAccessoryNone) {
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.delegate buViewController:self status:@"Multiple"];

    }else {
        newCell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    BOOL selected = NO;
    for (int i = 0; i < [_buTableView numberOfSections]; i++) {
        for (int j = 0; j < [_buTableView numberOfRowsInSection:i]; j++) {
            NSUInteger ints[2] = {i,j};
            NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
            UITableViewCell *cell = [_buTableView cellForRowAtIndexPath:indexPath];
            if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
                selected = YES;
                       
        }
    }
    if (selected) 
        [self.delegate buViewController:self status:@"Multiple"];
    else 
        [self.delegate buViewController:self status:@"None"];

}


- (IBAction)checkAll:(id)sender {
    
    checkAll = YES;
    uncheckAll = NO;
    for (int i = 0; i < [_buTableView numberOfSections]; i++) {
        for (int j = 0; j < [_buTableView numberOfRowsInSection:i]; j++) {
            NSUInteger ints[2] = {i,j};
            NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
            UITableViewCell *cell = [_buTableView cellForRowAtIndexPath:indexPath];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        }
    }
    
    
    [self.delegate buViewController:self status:@"All"];
    //NSLog(@"passing delgate check all");
   
}

- (IBAction)unCheckAll:(id)sender {
    
    uncheckAll = YES;
    checkAll = NO;
    for (int i = 0; i < [_buTableView numberOfSections]; i++) {
        for (int j = 0; j < [_buTableView numberOfRowsInSection:i]; j++) {
            NSUInteger ints[2] = {i,j};
            NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
            UITableViewCell *cell = [_buTableView cellForRowAtIndexPath:indexPath];
            cell.accessoryType = UITableViewCellAccessoryNone;
           
        }
    }
    
    [self.delegate buViewController:self status:@"None"];     
    //NSLog(@"passing delgate uncheck all");

}
@end
